<?php
namespace app\erp_meixingtiyu\logic;

use app\erp_meixingtiyu\model\Coach as CoachModel;
use app\erp_meixingtiyu\model\CourseCoach as CourseCoachModel;

class Coach
{
    public function getEmployeeList()
    {
        $list = (new CoachModel())->getList();
        return $list->toArray();
    }

    public function getInfoById($employee_id)
    {
      $employee = (new CoachModel)->findEmployeeById($employee_id);
      return $employee->toArray();
    }

    public function addEmployee($data)
    {
        $coach = (new CoachModel())->add($data);
        return $coach->toArray();
    }

    public static function updateEmployee($data)
    {
        $employee = CoachModel::updateInfo($data);
        return $employee->toArray();
    }

    public function deleteEmployee($employee_id)
    {
        // 判断该教练是否有课程，有则不允许删除
        // $courseNum = (new CourseModel)->countCoursesByEmployeeId($employee_id);
        $courseNum = (new CourseCoachModel)->hasCourses($employee_id);
        if($courseNum > 0) {
            throwError('该教练其下有课程，不能删除');
        }
        return (new CoachModel())->del($employee_id);
    }

    public function getAllCoach()
    {
        return (new CoachModel())->getAll()->visible(['employee_id', 'name'])->toArray();
    }
}
